/**
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY. 
 * See the GNU General Public License for more details.
 * 
 * Copyright (C) 2011 CellObject.net
 */
package net.cellobject.javacodeformatter.contribution;

import java.util.Collections;

import net.cellobject.javacodeformatter.log.FormatterLog;

import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.IHandler;
import org.eclipse.core.expressions.IEvaluationContext;
import org.eclipse.jface.action.ContributionItem;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.IViewSite;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.handlers.IHandlerService;

/**
 * Contribution that removes the currently selected files
 */
public class RemoveFormatterContributionItem extends ContributionItem {
	private final IViewSite viewSite;

	private final IHandler handler;

	boolean enabled = false;

	private MenuItem menuItem;

	private ToolItem toolItem;

	/**
	 * Construct a new contribution for the specified view
	 * 
	 * @param view the view (not <code>null</code>)
	 */
	public RemoveFormatterContributionItem(final IViewSite viewSite, final IHandler handler) {
		this.handler = handler;
		this.viewSite = viewSite;
		viewSite.getSelectionProvider().addSelectionChangedListener(new ISelectionChangedListener() {
			public void selectionChanged(final SelectionChangedEvent event) {
				enabled = !event.getSelection().isEmpty();
				updateEnablement();
			}
		});
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.jface.action.ContributionItem#fill(org.eclipse.swt.widgets .Menu, int)
	 */
	@Override
	public void fill(final Menu menu, final int index) {
		menuItem = new MenuItem(menu, SWT.NONE, index);
		menuItem.setText("Remove");
		menuItem.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(final SelectionEvent e) {
				run();
			}
		});
		updateEnablement();
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.jface.action.ContributionItem#fill(org.eclipse.swt.widgets .ToolBar, int)
	 */
	@Override
	public void fill(final ToolBar parent, final int index) {
		toolItem = new ToolItem(parent, SWT.NONE, index);
		toolItem.setToolTipText("Remove the selected files");
		toolItem.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(final SelectionEvent e) {
				run();
			}
		});
		updateEnablement();
	}

	/**
	 * Called when the view's selection changes so that the enabled state of the menu and toolbar items can be updated
	 * 
	 * @param enabled <code>true</code> if the contributions should be enabled
	 */
	private void updateEnablement() {
		final Image image = PlatformUI.getWorkbench().getSharedImages().getImage(
				enabled ? ISharedImages.IMG_TOOL_DELETE : ISharedImages.IMG_TOOL_DELETE_DISABLED);
		if (menuItem != null) {
			menuItem.setImage(image);
			menuItem.setEnabled(enabled);
		}
		if (toolItem != null) {
			toolItem.setImage(image);
			toolItem.setEnabled(enabled);
		}
	}

	/**
	 * Perform the operation by removing the currently selected Favorites items
	 */
	public void run() {
		// Setup execution context
		final IHandlerService handlerService = (IHandlerService)viewSite.getService(IHandlerService.class);
		final IEvaluationContext evaluationContext = handlerService.createContextSnapshot(true);
		final ExecutionEvent event = new ExecutionEvent(null, Collections.EMPTY_MAP, null, evaluationContext);
		try {
			handler.execute(event);
		} catch (final ExecutionException e) {
			FormatterLog.logError(e);
		}
	}
}
